﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Collections;
using GPCSC.Web;
using NickLee.Common;
using System.Text;
using System.IO;
using ExtAspNet;

namespace GPCSC.Web.sjsbyes
{
    public partial class ZTJS : PageBase
    {
        public double[] ad = new double[25];
        public StringBuilder[] adds = new StringBuilder[25];
        public string add = "0";


        public double[] su = new double[25];
        public StringBuilder[] sums = new StringBuilder[25];
        public string asum = "0";
        public string cout = "";
        protected void Page_Load(object sender, EventArgs e)
        {

            for (int i = 0; i < 25; i++)
            {
                adds[i] = new StringBuilder();
                sums[i] = new StringBuilder();
            }
            if (!IsPostBack)
            {
                F_bound();

            }
        }
        protected void F_bound()
        {
            Hashtable htable = new Hashtable();
            DataSet ds = new SqlReadXml().ReaderDataSet("sel_TJfile_all", htable);
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    Grid1.DataSource = ds.Tables[0];
                    Grid1.DataBind();
                }
            }
        }
        protected string GetEditUrl(object id)
        {
            return "javascript:" + Window1.GetShowReference("ZTJ_VIEW.aspx?id=" + id);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            load();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            int cout = Grid1.SelectedRowIndexArray.Length;
            if (cout == 0)
            {
                Alert.Show("请选择要导出的记录");
            }
            else if (cout > 1)
            {
                Alert.Show("您只能选择一条记录");
            }
            else
            {
                string ids = Grid1.DataKeys[0][0].ToString();
                string filename = geifile(ids);
                Response.Clear();
                System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
                System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
                //stringWriter.Write("<div>ceshi</div>");
                Response.BufferOutput = true;
                //设定输出的字符集
                //Response.Charset = "GB2312";
                //假定导出的文件名为FileName.doc
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
                //  Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                /**/
                ////设置导出文件的格式
                Response.ContentType = "application/ms-word";

                System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
                panl.RenderControl(textWriter);


                Response.Write(stringWriter.ToString());
                Response.End();
            }
        }
        protected string geifile(string id)
        {
            Hashtable htable = new Hashtable();
            htable.Add("@FileID", id);
            DataSet ds = new SqlReadXml().ReaderDataSet("tifilebyiod", htable);
            panl.InnerHtml = ds.Tables[0].Rows[0]["fileUrl"].ToString();

            return ds.Tables[0].Rows[0]["filename"].ToString();
        }
        protected void ttbSearch_Trigger1Click(object sender, EventArgs e)
        {
            F_bound();
        }
        protected void ttbSearch_Trigger2Click(object sender, EventArgs e)
        {

            string username = ttbSearch.Text.Trim();
            if (username == "")
            {
                F_bound();
            }
            else
            {
                Hashtable htable = new Hashtable();
                username = "%" + username + "%";
                htable.Add("@like", username);
                DataSet ds = new SqlReadXml().ReaderDataSet("sel_TJfile_bylike", htable);
                if (ds.Tables.Count > 0)
                {
                    Grid1.DataSource = ds.Tables[0];
                    Grid1.DataBind();
                }
            }
        }
        protected void load()
        {


            try
            {
                string postid = "";
                string postname = "";
                if (list.SelectedValue != "" && list.SelectedValue != null && list.SelectedValue != null)
                {
                    Hashtable htable = new Hashtable();
                    DataSet ds = new DataReturn().ReturnData(htable, "sel_POST_all");
                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {

                                foreach (DataRow dr in ds.Tables[0].Rows)
                                {
                                    for (int i = 0; i < 25; i++)
                                    {
                                        adds[i] = new StringBuilder();
                                        sums[i] = new StringBuilder();
                                        ad[i] = 0;
                                        su[i] = 0;
                                    }
                                    add = "0";
                                    asum = "0";
                                    cout = "";
                                    postid = dr["CL_POST_SOID"].ToString();
                                    postname = dr["POSTNAME"].ToString();
                                    f_buildGrid(postid, postname, list.SelectedValue);
                                }
                            }
                        }
                    }
                }
                F_bound();
                Alert.Show("汇总成功");
            }
            catch (Exception ee)
            {
                throw ee;
            }
         
           

        }


        protected void f_buildGrid(string postid, string postname,string month)
        {
            this.postid.Value = postid;
            this.postName.Value = postname;
           
            DataSet dsname = new Summ().PostUser(postid);
            sumss(dsname, "newSelAllbyuserid_km", month, month);

            creat(postname,postid,month);
         
        }
        protected void sumss(DataSet dsname, string sql, string kmonth, string jmonth)
        {


            if (dsname != null)
            {
                if (dsname.Tables.Count > 0)
                {
                    if (dsname.Tables[0].Rows.Count > 0)
                    {
                        PostListS.DataSource = dsname;
                        PostListS.DataBind();
                        cout = dsname.Tables[0].Rows.Count.ToString();
                        //循环遍历每个领导人
                        foreach (DataRow dr in dsname.Tables[0].Rows)
                        {
                            string userid = dr["CL_USER_SOID"].ToString();
                            Hashtable htables = new Hashtable();
                            htables.Add("@lgid", userid);
                            htables.Add("@year", DateTime.Now.Year);
                            htables.Add("@kmonth", kmonth);
                            htables.Add("@jmonth", jmonth);
                            DataSet ds = new SqlReadXml().ReaderDataSet(sql, htables);
                            if (ds != null)
                            {
                                if (ds.Tables.Count > 0)
                                {
                                    if (ds.Tables[0].Rows.Count > 0)
                                    {
                                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                                        {
                                            ad[0] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["zday"].ToString());//下基层活动天数
                                            ad[1] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XJCDYTS"].ToString());//下基层调研
                                            ad[2] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZXMQRJPS"].ToString());//民情
                                            ad[3] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZCRHTS"].ToString());//走村入户
                                            ad[4] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDZHTS"].ToString());//驻点住户
                                            ad[5] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["GKJFTS"].ToString());//公开接访天数
                                            ad[6] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["JDXFAJ"].ToString());//接待信访件数
                                            ad[7] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["yjaxf"].ToString());//已结案
                                            ad[8] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJS"].ToString());//重点信访
                                            ad[9] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZDXFBAJWCS"].ToString());//重点信访包案。。
                                            ad[10] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["KNQZZHS"].ToString());//已走访困难
                                            ad[11] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWKNQZQK_YZFHS"].ToString());//已走访挂钩。。
                                            ad[12] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["YBFHS"].ToString());// 已帮扶挂钩。。
                                            ad[13] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WWJZZHLJJE"].ToString());//慰问捐助金额。。
                                            ad[14] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["ZKZTHCS"].ToString());
                                            ad[15] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["Z_CJRC"].ToString());
                                            ad[16] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["XSZCXJCS"].ToString());
                                            ad[17] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["X_CJRC"].ToString());
                                            ad[18] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["CJLDTS"].ToString());
                                            ad[19] += Usual.f_convertDobule(ds.Tables[0].Rows[i]["WJCDWFCZJLJJE"].ToString());
                                        }
                                        for (int i = 0; i < 20; i++)
                                        {
                                            adds[i].Append("<td  class=\"td\" >" + ad[i].ToString() + "</td>");
                                            su[i] += ad[i];
                                            ad[i] = 0;
                                        }
                                    }
                                    else
                                    {
                                        for (int m = 0; m < 20; m++)
                                        {

                                            ad[m] = 0;
                                            adds[m].Append("<td  class=\"td\" >" + add + "</td>");
                                        }
                                    }
                                }
                            }
                            adwt(dr["CL_USER_SOID"].ToString(), kmonth, jmonth);//问题总计
                            jjwt(dr["CL_USER_SOID"].ToString(), kmonth, jmonth);//解决问题统计
                            Bss(dr["CL_USER_SOID"].ToString(), kmonth, jmonth);//办实事
                            knAgg(dr["CL_USER_SOID"].ToString());
                            


                        }
                        //合计值
                        for (int i = 0; i < 25; i++)
                        {

                            sums[i].Append("<td  class=\"td\" >" + su[i].ToString() + "</td>");
                        }
                    }
                }
            }

        }

        // 问题统计
        protected void adwt(string userid, string kmonth, string jmonth)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", DateTime.Now.Year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("countWT", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["wts"].ToString());
            }
            adds[20].Append("<td  class=\"td\" >" + wts.ToString() + "</td>");
            su[20] += wts;
        }

        //解决问题统计
        protected void jjwt(string userid, string kmonth, string jmonth)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", DateTime.Now.Year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("countjjWT", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["jwts"].ToString());
            }
            adds[21].Append("<td  class=\"td\" >" + wts.ToString() + "</td>");
            su[21] += wts;
        }
        //办实事情况
        protected void Bss(string userid, string kmonth, string jmonth)
        {
            int wts = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            htable.Add("@year", DateTime.Now.Year);
            htable.Add("@kmonth", kmonth);
            htable.Add("@jmonth", jmonth);
            DataSet dwts = new SqlReadXml().ReaderDataSet("counBss", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {
                wts = Usual.f_convertInt32(dwts.Tables[0].Rows[0]["jwts"].ToString());
            }
            adds[22].Append("<td  class=\"td\" >" + wts.ToString() + "</td>");
            su[22] += wts;
        }

        //困难及挂钩结对户数
        protected void knAgg(string userid)
        {
            int kns = 0;
            int ggs = 0;
            Hashtable htable = new Hashtable();
            htable.Add("@lgid", userid);
            DataSet dwts = new SqlReadXml().ReaderDataSet("selks", htable);
            if (dwts.Tables[0].Rows.Count > 0)
            {

                foreach (DataRow dr in dwts.Tables[0].Rows)
                {
                    kns += Usual.f_convertInt32(dr["knqzzhs"].ToString());
                    ggs += Usual.f_convertInt32(dr["ggjdhs"].ToString());

                }
            }
            adds[23].Append("<td  class=\"td\" >" + kns.ToString() + "</td>");
            su[23] += kns;
            adds[24].Append("<td  class=\"td\" >" + ggs.ToString() + "</td>");
            su[24] += ggs;

        }

        protected void creat(string postname, string postid,string months)
        {
            string filename = postname + DateTime.Now.Year.ToString() + "年" + months + "月" + "三进三帮统计表.doc";
            //Response.Clear();
            //Response.BufferOutput = true;
            ////设定输出的字符集
            //Response.Charset = "GB2312";
            ////假定导出的文件名为FileName.doc
            //Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
            //Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            /**/
            ////设置导出文件的格式
            //Response.ContentType = "application/ms-word";
            //关闭ViewState


            // GrwResult.EnableViewState = false;
            Panel1.EnableViewState = false;


            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);

            Panel1.RenderControl(textWriter);

            //保存到服务器中
            SQLServerDAL.databyhtable dtable = new SQLServerDAL.databyhtable();
            Hashtable htable = new Hashtable();
            htable.Add("fileUrl", stringWriter.ToString());
            htable.Add("TJdate", DateTime.Now);
            #region  首先验证该月是否有 记录

            string year = DateTime.Now.Year.ToString();
            string month = months;
            Hashtable htab = new Hashtable();
            htab.Add("@year", year);
            htab.Add("@month", month);
            htab.Add("@POstID", postid);

            DataSet ds = new SqlReadXml().ReaderDataSet("tifiled", htab);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dtable.databyhtables(htable, "update", "TJfile", "FileID", ds.Tables[0].Rows[0]["FileID"].ToString());
            }
            else
            {
                htable.Add("filename", filename);
               
                htable.Add("POstID", postid);
                htable.Add("year", DateTime.Now.Year);
                htable.Add("month", month);
                htable.Add("PostName", postname);
                dtable.databyhtables(htable, "add", "TJfile", "", "");
            }
            #endregion
        }
    }
}